package com.zmj.solr.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class FileUtils {


    public static String check(CommonsMultipartFile file) {
        String sourceName = file.getOriginalFilename(); // 原始文件名
        String fileType = sourceName.substring(sourceName.lastIndexOf("."));
        if (file.isEmpty() || StringUtils.isBlank(fileType)) {
            return "文件不能为空";
        }
        if (!".txt".equals(fileType.toLowerCase())) {
            return "文件必须是txt格式";
        }
        return null;
    }

    public static void writeLines(File file, List<String> readLines, boolean append,String updateBy) throws IOException {
        org.apache.commons.io.FileUtils.writeLines(file,"UTF-8",readLines,append);
    }


    //插入注释行
    public static void writeRemarkLine(File file, boolean append,String updateBy) throws IOException {
        org.apache.commons.io.FileUtils.write(file, "\n", "UTF-8",append);
        org.apache.commons.io.FileUtils.write(file, remarkLine( updateBy),"UTF-8", append);

    }

    public static File  mkdir(String basePath,String uploadPath) {
        File tempFile = new File(basePath + uploadPath);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        return tempFile;
    }

    public static String remarkLine(String updateBy){
        StringBuffer sb = new StringBuffer("#updateDate : ");
        sb.append( updateDate());
        sb.append(",updateby : ");
        sb.append(updateBy);
        return sb.toString();
    }
    public static String updateDate(){
       return DateFormatUtils.format(System.currentTimeMillis(), DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.getPattern()).toString();
    }
}
